﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using XStudio.XCRF.Data.Business;
using XStudio.XCRF.Data.Business.Archieve;
using XStudio.XCRF.Data.Business.Validate;
using XStudio.XCRF.Data.Enums;
using System.Linq;
using System;

namespace XStudio.XCRF.Data.Entities
{
    /// <summary>
    /// 医院
    /// </summary>
    [HospitalValidate()]
    public partial class Hospital : BaseEntity<Hospital>, IArchieve
    {
        #region 属性
        /// <summary>
        /// 医院编号
        /// </summary>
        [ArchieveField]
        [Required]
        [Column(TypeName="varchar")]
        public string Code { get; set; }
        /// <summary>
        /// 医院名称
        /// </summary>
        [ArchieveField]
        [Column(TypeName = "varchar")]
        [MaxLength(50)]
        public string Name { get; set; }
        /// <summary>
        /// 医院地址
        /// </summary>
        [ArchieveField]
        [Column(TypeName = "varchar")]
        [MaxLength(100)]
        public string Address { get; set; }
        /// <summary>
        /// 联系电话
        /// </summary>
        [ArchieveField]
        [DataType(DataType.PhoneNumber)]
        [Column(TypeName="varchar")]
        public string Phone { get; set; }
        /// <summary>
        /// 电子邮件
        /// </summary>
        [ArchieveField]
        [DataType(DataType.EmailAddress)]
        [Column(TypeName = "varchar")]
        public string Email { get; set; }
        /// <summary>
        /// 网址
        /// </summary>
        [ArchieveField]
        [DataType(DataType.Url)]
        [Column(TypeName = "varchar")]
        [MaxLength(100)]
        public string Website { get; set; }
        /// <summary>
        /// 详细介绍
        /// </summary>
        [ArchieveField]
        [Column(TypeName = "varchar")]
        [DataType(DataType.MultilineText)]
        public string Detail { get; set; }
        /// <summary>
        /// 成员列表
        /// </summary>
        public virtual ICollection<Member> Members { get; set; }
        /// <summary>
        /// 医院的病例报告表集合
        /// </summary>
        public virtual ICollection<CRF> CRFs { get; set; }

        /// <summary>
        /// 医院标准值
        /// </summary>
        [InverseProperty("Hospital")]
        public virtual ICollection<LabCheckStd> LabCheckStds { get; set; }
        #endregion 属性

        #region 业务方法

        /// <summary>
        /// 获取医院主管
        /// </summary>
        [NotMapped]
        public Member Owner
        {
            get
            {
                Member owner = (from member in Members
                                where member.Type == (byte)MemberTypes.Administrator || member.Type == (byte)MemberTypes.Owner
                                select member).GetMasterRecords().FirstOrDefault();
                return owner;
            }
        }
        /// <summary>
        /// 判断医院是否拥有主管
        /// </summary>
        [NotMapped]
        public bool HasOwner
        {
            get
            {
                if (Members == null)
                    return false;
                bool has = (from member in Members
                            where member.Type == (byte)MemberTypes.Administrator || member.Type == (byte)MemberTypes.Owner
                            select member).GetMasterRecords().Any();
                return has;
            }
        }
        /// <summary>
        /// 判断医院是否是核心医院（建站医院）
        /// </summary>
        [NotMapped]
        public bool IsHost
        {
            get
            {
                if (Members == null)
                    return false;
                bool isHost = (from member in Members
                               where member.Type == (byte)MemberTypes.Administrator
                               select member).GetMasterRecords().Any();
                return isHost;
            }
        }
        /// <summary>
        /// 判断是否是第一次登录
        /// </summary>
        [NotMapped]
        public bool IsFirst { get { return CreateTime == LastModifyTime; } }
        #endregion 业务方法
    }
}
